﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using System.Xml;
using StuMana.Services;

namespace StuMana.UI
{
    public partial class SchoolLate1 : Office2007Form
    {
         #region Variables
        // datagridview parameters
        List<string>[] list;
        string[] columnName;
        string query;
        string[] columnNameReal;
        int page;
        int maxpage;
        static int rowperpage;

        private Services.DataviewService dataview = new Services.DataviewService();

        // Timer kiem tra thoi gian
        private System.Windows.Forms.Timer readTimer;

        // lay thoi gian
        DateTime dt = new DateTime();

        #endregion
        
        string ProPath;
        byte[] serial_buff = new byte[40];
        Services.SL025M rfid_reader = new Services.SL025M();
        Services.COM connect;
        private Bitmap image;
    //    private System.Windows.Forms.Timer readTimer;
        private string tag = "";
        private Boolean isSelect = false;
        private Services.student print_std = new Services.student();
        // get size of return byteArray
        private int size;

 // Initial COM connecting
        public void InitialConnect(StuMana.Services.COM conn)
        {
            connect = conn;
            rfid_reader.conn = conn.serialPort;
        }
  
        private bool flag = false;
        private bool co = false;
        private Font fontForAll = null;
        private StyleManager managerStyle=new DevComponents.DotNetBar.StyleManager();

        public void ReadSetting()
        {
            XmlReader reader = XmlReader.Create("Settings.xml");
            while (reader.Read())
            {
                if (reader.NodeType == XmlNodeType.Element
                && reader.Name == "font")
                {
                    string name=reader.GetAttribute(0).ToString();
                    string style = reader.GetAttribute(1).ToString();
                    float size = float.Parse(reader.GetAttribute(2).ToString());

                    if(style == "Bold")
                       fontForAll = new System.Drawing.Font(reader.GetAttribute(0).ToString(),size,System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    if(style == "Italic")
                        fontForAll = new System.Drawing.Font(reader.GetAttribute(0).ToString(), size, System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    if(style == "Regular")
                        fontForAll = new System.Drawing.Font(reader.GetAttribute(0).ToString(),size,System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    if(style == "Strikeout")
                        fontForAll = new System.Drawing.Font(reader.GetAttribute(0).ToString(), size, System.Drawing.FontStyle.Strikeout, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    if(style == "Underline")
                        fontForAll = new System.Drawing.Font(reader.GetAttribute(0).ToString(), size, System.Drawing.FontStyle.Underline, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    
                    while (reader.NodeType != XmlNodeType.EndElement)
                    {
                        reader.Read();
                        if (reader.Name == "managerstyle" && reader.NodeType == XmlNodeType.Element)
                        {
                            reader.Read();
                            string stylemana=reader.Value.ToString();

                            if(stylemana=="Office2007Blue")
                                managerStyle.ManagerStyle = DevComponents.DotNetBar.eStyle.Office2007Blue;
                            if(stylemana=="Office2007Silver")
                                managerStyle.ManagerStyle = DevComponents.DotNetBar.eStyle.Office2007Silver;
                            if(stylemana=="Office2007Black")
                                managerStyle.ManagerStyle = DevComponents.DotNetBar.eStyle.Office2007Black;
                            if(stylemana=="Windows7Blue")
                                managerStyle.ManagerStyle = DevComponents.DotNetBar.eStyle.Windows7Blue;
                            if(stylemana=="Office2007VistaGlass")
                                managerStyle.ManagerStyle = DevComponents.DotNetBar.eStyle.Office2007VistaGlass;
                            if(stylemana=="Office2010Silver")
                                managerStyle.ManagerStyle = DevComponents.DotNetBar.eStyle.Office2010Silver;
                            if(stylemana=="Office2010Blue")
                                managerStyle.ManagerStyle = DevComponents.DotNetBar.eStyle.Office2010Blue;
                            if(stylemana=="Office2010Black")
                                managerStyle.ManagerStyle = DevComponents.DotNetBar.eStyle.Office2010Black;
                        }                
                    }
                } 
            }
            reader.Close();
        }

        public void ConfigSetting()
        {
            ReadSetting();
            for(int i=0;i<Controls[0].Controls[0].Controls[1].Controls.Count;i++) 
            {
                if (Controls[0].Controls[0].Controls[1].Controls[i] is LabelX)
                {
                    Controls[0].Controls[0].Controls[1].Controls[i].Font = fontForAll;
                }
            }

            styleManager1 = managerStyle;
        }
        private void LoadDanhSachSV(int type)
        {
            // type = 0 ;
            ConnectionController control = new ConnectionController();
            control.connection.Open();
            try
            {
                MySql.Data.MySqlClient.MySqlCommand cmd = control.connection.CreateCommand();
                cmd.CommandText = "SELECT *  FROM studentinfo WHERE InOut_status = " + type.ToString();
                MySql.Data.MySqlClient.MySqlDataAdapter adap = new MySql.Data.MySqlClient.MySqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                adap.Fill(ds);
                dgvExDanhSach.DataSource = ds.Tables[0].DefaultView;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (control.connection.State == ConnectionState.Open)
                    control.connection.Clone();
            }

        }
        private void Cafeteria1_Load(object sender, EventArgs e)
        {
           
            flag = true;
            ConfigSetting();
            co = true;


            
            
        }
  
       

        public SchoolLate1()
        {
            InitializeComponent();
           schoolLate_initial();
        }
        
       

        // initials
        public void schoolLate_initial()
        {

            // timer 
            //timer setting
            readTimer = new System.Windows.Forms.Timer();
            readTimer.Interval = 1000;
            readTimer.Tick += new EventHandler(readTimer_Tick);
            readTimer.Enabled = true;

            // mysql command
            columnName = new string[] { "TagID", "Name", "Class", "LateTime","InOut_status" };
            columnNameReal = new string[] { "TagID", "Name", "Class", "LateTime", "InOut_status" };
            query = "SELECT TagID, Name, Class, LateTime, InOut_status FROM studentinfo WHERE InOut_status=0";

            LoadDanhSachSV(0);
            
        }

        /*
         * Timer handling
         */

        private void readTimer_Tick(object sender, EventArgs e)
        {
            dt = DateTime.Now;
            lblTime.Text = dt.ToLongTimeString();

            LoadDanhSachSV(0);


            /*
             * Demo for listing school late students
             */
            if (dt.Second == int.Parse(txtDemoMinute.Text))
            {
                int n = 0;
                string showInfor = "School late list at 07:00:00:\n";
                list = dataview.GetList();
                while (n != list[0].Count)
                {
                    if (list[3][n].ToString() == "07:00:00")
                        showInfor += list[1][n].ToString() + " " + list[3][n].ToString() + " \n";
                    n++;
                }
                MessageBox.Show(showInfor, "Warning");
            }

        }

      
        private void SchoolLate1_Load(object sender, EventArgs e)
        {
            ConfigSetting();
            LoadDanhSachSV(0);
            co = true;
            flag = true;
        }

        private void SchoolLate1_FormClosed(object sender, FormClosedEventArgs e)
        {
          Application.ExitThread();
            Application.Exit();
        }

        private void SchoolLate1_FormClosing(object sender, FormClosingEventArgs e)
        {
          Application.ExitThread();
            Application.Exit();
        }

        private void SchoolLate1_Resize(object sender, EventArgs e)
        {
            int w = dgvExDanhSach.Size.Width;
            int widthC = ((int)w /8);
            if (co == true)
            {
                dgvExDanhSach.Columns[0].Width = widthC;
                dgvExDanhSach.Columns[1].Width = widthC;
                dgvExDanhSach.Columns[2].Width = widthC;
                dgvExDanhSach.Columns[3].Width = widthC;
                dgvExDanhSach.Columns[4].Width = widthC;
                dgvExDanhSach.Columns[5].Width = widthC;
                dgvExDanhSach.Columns[6].Width = widthC;   
                dgvExDanhSach.Columns[7].Width = widthC;   
            }
            
        }
    }
}
